package com.shangjinrong.controller.site;

import com.shangjinrong.domain.UserAccount;
import com.shangjinrong.domain.UserInfo;
import com.shangjinrong.domain.dto.LoginDto;
import com.shangjinrong.domain.vo.UserIndexVO;
import com.shangjinrong.domain.vo.UserInfoVO;
import com.shangjinrong.excption.BusinessException;
import com.shangjinrong.result.R;
import com.shangjinrong.result.ResponseEnum;
import com.shangjinrong.service.UserAccountService;
import com.shangjinrong.service.UserInfoService;
import com.shangjinrong.util.JwtUtils;
import com.shangjinrong.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("userInfo")
public class SiteUserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserAccountService userAccountService;


    @GetMapping("checkMobile/{mobile}")
    public R selectByMobile(@PathVariable String mobile) {
        UserInfo userInfo = userInfoService.selectByMobile(mobile);
        if (userInfo == null) {
            return R.ok().data("data", true);
        }
        return R.error().data("data", false).message("该手机号已注册,请更换手机号");
    }

    @PostMapping("register")
    @Transactional(rollbackFor = Exception.class)
    public R register(@RequestBody UserInfo userInfo) {
        userInfo.setPassword(MD5.encrypt(userInfo.getPassword()));
        boolean save = userInfoService.save(userInfo);

        if (save) {
            UserInfo info = userInfoService.selectByMobile(userInfo.getMobile());
            //插入用户账户表：user_account
            UserAccount userAccount = new UserAccount();
            userAccount.setUserId(info.getId());
            userAccountService.save(userAccount);

            return R.ok().message("注册成功").data("data", true);
        }
        return R.error().message("注册失败").data("data", false);
    }

    @PostMapping("login")
    public R login(@RequestBody LoginDto loginDto) {

        try {
            UserInfoVO login = userInfoService.login(loginDto);
            if (login == null) {
                return R.error().message("手机号或密码错误,请重新输入");
            }
            return R.ok().message("登录成功").data("data", login);
        } catch (Exception e) {
            throw new BusinessException(ResponseEnum.LOGIN_PASSWORD_ERROR);
        }

    }

    @GetMapping("selectById")
    public R selectById(HttpServletRequest request){
        String token = request.getHeader("token");
        long userid=JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userid);
        return R.ok().data("userInfo",userInfo);
    }
    @PutMapping("updateUserInfo")
    public  R saveUserInfo(HttpServletRequest request,@RequestBody UserInfo userInfo){
        try {
            String token = request.getHeader("token");
            long userid=JwtUtils.getUserId(token);
            userInfo.setId(userid);
            userInfoService.updateById(userInfo);
        }catch (Exception e){
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR);
        }
        return R.ok().message("上传成功");
    }

    //校验令牌
    @GetMapping("checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        boolean b = JwtUtils.checkToken(token);
        if (b) {
            return R.ok();
        } else {
            return R.error().message("未登录");
        }
    }

    @GetMapping("getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request){
        String token=request.getHeader("token");
        long  userId=JwtUtils.getUserId(token);
        UserIndexVO userIndexVO=userInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVO",userIndexVO);
    }



}
